package com.springseries.websocket.service;

import com.springseries.websocket.common.Constants;
import com.springseries.websocket.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebsocketService {

    @Autowired(required = false)
    private SimpMessagingTemplate template;

    /**
     * 广播
     *
     * @param users
     * @param msg
     */
    public void sendMsg(List<String> users, Message msg){
        template.convertAndSend(Constants.PRODUCER_PATH,msg);
    }

    /**
     * 指定用户播放
     *
     * @param users
     * @param msg
     */
    public void sendToUser(List<String> users, Message msg){
        users.forEach(userName ->{
            template.convertAndSendToUser(userName, Constants.P2P_PUSH_PATH, msg);
        });
    }
}
